package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //菜品表插入一个菜品数据
        Dish dish = new Dish();
        //属性复制
        BeanUtils.copyProperties( dishDTO,dish );
        dishMapper.insert(dish);

        //口味表批量插入口味数据
        //获取dish_id
        Long dishid = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            flavors.forEach( dishFlavor -> {
                dishFlavor.setDishId( dishid );
            } );
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dto) {
        //开始分页查询
        PageHelper.startPage( dto.getPage(),dto.getPageSize() );
        Page<DishVO> page = dishMapper.pageQuery(dto);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除菜品
     * @param ids
     */
    public void delete(List<Long> ids) {
//        是否在售卖中
        ids.forEach( id -> {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE) {
                    throw new DeletionNotAllowedException( MessageConstant.DISH_ON_SALE );
            }
        } );
//        是否有关联的套餐
           List<Long> list =  setmealDishMapper.getMealByDishId(ids);
           if(list == null ||  list.size() > 0) {
               throw  new DeletionNotAllowedException( MessageConstant.DISH_BE_RELATED_BY_SETMEAL );
           }
////        删除菜品
//        ids.forEach( id -> {
//            dishMapper.delete(id);
//            //        删除关联的口味
//            dishFlavorMapper.delete(id);
//        } );
    //批量删除菜品
        dishMapper.deleteByIds(ids);
    //批量删除关联的口味数据
        dishFlavorMapper.deleteByIds(ids);



    }

    /**
     * 根据ID查询菜品
     * @param id
     * @return
     */
    public DishVO getByIdWithFlavor(Long id) {
        //查询菜品
        Dish dish = dishMapper.getById( id );

        //查询口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByid(id);

        //封装
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties( dish,dishVO );
        dishVO.setFlavors( dishFlavors );

        return dishVO;
    }

    /**
     * 修改菜品和口味信息
     * @param dishDTO
     */
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties( dishDTO,dish );
        //修改菜品信息
        dishMapper.update(dish);
        //删除口味数据
        dishFlavorMapper.delete( dishDTO.getId() );
        //插入口微数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            flavors.forEach( dishFlavor -> {
                dishFlavor.setDishId( dishDTO.getId() );
            } );
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     */
    @Transactional
    public void updateDishStatue(Integer status,Long id) {
//        Long id = BaseContext.getCurrentId();
        Dish dish = Dish.builder()
                .status( status )
                .id( id )
                .build();
        dishMapper.update( dish );
        if(status == StatusConstant.DISABLE) {
            //修改套餐
            List<Long> dishIds = new ArrayList<>();
            dishIds.add( id );
            List<Long> mealIds = setmealDishMapper.getMealByDishId( dishIds );
            if(mealIds != null && mealIds.size() > 0) {
                mealIds.forEach( mealId ->{
                    Setmeal setmeal = Setmeal.builder()
                            .id(mealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);

                });
            }
        }
    }

    /**
     * 根据分类id查询菜品
     *
     * @param id
     * @return
     */
    public List<Dish> selectByCategoryId(Long id) {
        //获取所有菜品 select * from Dish where CategoryId = #{id} name = #{name} status = #{status}
        //必须是对应名字并且出售和在分类里面的菜品
        Dish dish = Dish.builder()
                .status( StatusConstant.ENABLE )
                .categoryId( id )
                .build();
        return  dishMapper.selectByCategoryId(dish);
//        return null;
    }

    /**
     * 根据分类id查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> getByCategoryIdWithFlavor(Dish dish) {
        //获取菜品
        List<Dish> dishList = dishMapper.selectByCategoryId( dish );
        //属性拷贝
        List<DishVO> dishVO = new ArrayList<>();
        dishList.forEach( dish1 ->{
            DishVO dishVO1 = new DishVO();
            List<DishFlavor> dishFlavors = dishFlavorMapper.getByid( dish1.getId() );
            dishVO1.setFlavors( dishFlavors );
            BeanUtils.copyProperties( dish1,dishVO1 );
            dishVO.add( dishVO1 );
        } );
        return dishVO;
    }
}
